#include<iostream>
#include<string>
#include<unordered_map>
#include<queue>
#include<algorithm>
using namespace std;
char g[2][4];
unordered_map<string, pair<char, string>>pre;
unordered_map<string, int>dis;
queue<string>q;
void set(string start)
{
	for (int i = 0; i < 4; i++) {
		g[0][i] = start[i];
	}
	for (int i = 3, j = 4; i >= 0; i--, j++) {
		g[1][i] = start[j];
	}
}
string get()
{
	string ans;
	for (int i = 0; i < 4; i++) {
		ans += g[0][i];
	}
	for (int i = 3; i >= 0; i--) {
		ans += g[1][i];
	}
	return ans;
}
string move0(string start)
{
	set(start);
	for (int i = 0; i < 4; i++) {
		swap(g[0][i], g[1][i]);
	}
	return get();
}
string move1(string start)
{
	set(start);
	char v1 = g[0][3];
	char v2 = g[1][3];
	for (int i = 0; i < 2; i++) {
		for (int j = 3; j >= 1; j--) {
			g[i][j] = g[i][j - 1];
		}
	}
	g[0][0] = v1;
	g[1][0] = v2;
	return get();
}
string move2(string start)
{
	set(start);
	char v = g[0][1];
	g[0][1] = g[1][1];
	g[1][1] = g[1][2];
	g[1][2] = g[0][2];
	g[0][2] = v;
	return get();
}
void bfs(string start, string end)
{
	if (start == end) {
		return;
	}
	q.push(start);
	dis[start] = 0;
	while (q.size()) {
		string t = q.front();
		q.pop();
		string str[3];
		str[0] = move0(t);
		/*for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 4; j++) {
				cout << g[i][j] << " ";
			}
			cout << endl;
		}*/
		str[1] = move1(t);
		str[2] = move2(t);
		bool flag = 0;
		/*for (int i = 0; i < 3; i++) {
			cout << "->  " << str[i] << endl;
		}
		break;*/
		for (int i = 0; i < 3; i++) {
			if (dis.count(str[i]) == 0) {
				pre[str[i]] = { char(i + 'A'),t };
				dis[str[i]] = dis[t] + 1;
				q.push(str[i]);
				if (str[i] == end) {
					flag = 1;
					break;
				}
			}
		}
		if (flag) {
			break;
		}
	}
}
int main()
{
	int x;
	string start, end;
	for (int i = 0; i < 8; i++) {
		cin >> x;
		end += (char)(x + '0');
	}
	for (int i = 0; i < 8; i++) {
		start += (char)(i + '1');
	}
	/*cout << start << endl;
	cout << end << endl;*/
	bfs(start, end);
	string res;
	cout << dis[end] << endl;
	/*a A b
	c B d
	e C f
	h A z
	h*/
	while (start != end) {
		res += pre[end].first;
		end = pre[end].second;
	}
	reverse(res.begin(), res.end());
	if (res.size()) {
		cout << res << endl;
	}
	return 0;
}